'Copyright (c) 2009, Code Endeavors, LLC - 'http://www.codeendeavors.com
'This source is subject to the BSD License.
'See http://www.opensource.org/licenses/bsd-license.php

Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection
Imports DotNetNuke.Entities.Modules.Actions

Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.UI.Utilities
Imports DotNetNuke.Entities.Modules

Namespace DotNetNuke.Modules.Workflow

    <ControlMethodClass("OliverHine.Modules.Workflow.ViewWorkflow")> _
    Partial Class ViewWorkflow
        Inherits AjaxPortalModuleBase
        Implements IActionable

#Region "AjaxPortalModuleBase Events"
        '-------------------------------------------------------------------------------------------------------------
        '- This is where you add your custom properties that are to be sent down to the client-side object
        '- By default we are passing the naming container ID (ns) and localized messages (msgs) 
        '- If you add your own properties make sure you update your client code with those properties found in EditWorkflow.ascx.js
        '-      get_ImagePath: function() {return this._imagePath;},
        '-      set_ImagePath: function(value) {this._imagePath = value;},
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddScriptComponentDescriptors(ByVal Descriptor As ScriptComponentDescriptor) Handles MyBase.AddScriptComponentDescriptors
            'IMPORTANT!  Enter Client Namespace + ObjectName as Type
            Descriptor.Type = "OliverHine.ViewWorkflow"

            '---------------------------------------------------------------------------------------------------------
            'Add custom properties here
            '   Descriptor.AddScriptProperty("ImagePath", String.Format("'{0}'", Me.ModulePath & "images/"))
            '---------------------------------------------------------------------------------------------------------
        End Sub

        '-------------------------------------------------------------------------------------------------------------
        '- This is where your client-side javascript that uses the MS AJAX framework needs to be registered
        '- Adding the reference here ensures that the MS AJAX script is run before our script which uses things like Type.registerNamespace run
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddScriptReferences(ByVal References As List(Of ScriptReference)) Handles MyBase.AddScriptReferences
            References.Add(New System.Web.UI.ScriptReference(Me.ControlPath & "ViewWorkflow.ascx.js"))
        End Sub

        '-------------------------------------------------------------------------------------------------------------
        '- Add any localized text needed on the client
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddLocalizedMessages(ByVal Messages As Dictionary(Of String, String)) Handles MyBase.AddLocalizedMessages
            Messages("btnHello") = Localization.GetString("btnHello.Client", LocalResourceFile)
        End Sub

#End Region

        Protected m_controlToLoad As String = "ManageWorkflow.ascx"
        Protected m_LoadedControl As Entities.Modules.PortalModuleBase

        Protected Sub LoadControlType()

            If m_controlToLoad <> "" Then

                m_LoadedControl = CType(Me.LoadControl(m_controlToLoad), Entities.Modules.PortalModuleBase)

                If Not (m_LoadedControl Is Nothing) Then

                    m_LoadedControl.ModuleConfiguration = Me.ModuleConfiguration
                    m_LoadedControl.ID = System.IO.Path.GetFileNameWithoutExtension(m_controlToLoad)
                    'm_LoadedControl.Dispatcher = Me
                    Controls.Add(m_LoadedControl)

                End If
            End If

        End Sub

        Protected Sub ReadSettings()
            If Me.Settings.ContainsKey("DefaultControl") Then

                Select Case Me.Settings("DefaultControl").ToLowerInvariant()

                    Case "manage"
                        m_controlToLoad = "ManageWorkflow.ascx"
                    Case "mywork"
                        m_controlToLoad = "MyWork.ascx"

                End Select

            End If
        End Sub

#Region "Event Handlers"

        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            ' Assign parameters
            '
            ReadSettings()

            ' Load appropriate type
            '
            LoadControlType()

            'If (m_controlToLoad <> "") AndAlso (m_controlToLoad <> DefaultControl) Then
            Dim strNewModuleTitle As String = Localization.GetString(m_controlToLoad, LocalResourceFile)
            If Not String.IsNullOrEmpty(strNewModuleTitle) Then ModuleConfiguration.ModuleTitle = strNewModuleTitle
            'End If
        End Sub


#End Region

#Region "Control Methods"
        '-------------------------------------------------------------------------------------------------------------
        '- Exposing methods to the client code is as simple as adding the ControlMethod attribute
        '- IMPORTANT!   ALWAYS SECURE YOUR CONTROL METHODS IF INTERACTING WITH SENSITIVE DATA
        '-------------------------------------------------------------------------------------------------------------
        <ControlMethod()> _
        Public Function Hello(ByVal Name As String, ByVal Description As String) As String
            Return String.Format("Hello {0}, your description was submitted at {1}", Name, Now.ToString())
        End Function
#End Region

        Public ReadOnly Property ModuleActions() As ModuleActionCollection Implements IActionable.ModuleActions
            Get
                Return New ModuleActionCollection() From {
                        New ModuleAction(GetNextActionID(), Localization.GetString("My Work.Action", LocalResourceFile), "MyWork", String.Empty, String.Empty, EditUrl("MyWork"))
                    }
            End Get
        End Property
    End Class

End Namespace
